package com.heima.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult findSensitive(SensitiveDTO dto) {
//        参数校验
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
//        分页查询
        IPage<AdSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.likeRight(AdSensitive::getSensitives,dto.getName());
        }
        IPage<AdSensitive> sensitiveIPage = page(page, wrapper);
        return new PageResponseResult(dto.getPage(), dto.getSize(),sensitiveIPage.getTotal(),sensitiveIPage.getRecords());
    }

    @Override
    public ResponseResult addSensitive(AdSensitive adSensitive) {
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int count = count(new LambdaQueryWrapper<AdSensitive>().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives()) || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int countID = count(new LambdaQueryWrapper<AdSensitive>().eq(AdSensitive::getId, adSensitive.getId()));
        if (countID == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        int countS = count(new LambdaQueryWrapper<AdSensitive>().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (countS > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");
        }
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delSensitive(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int countID = count(new LambdaQueryWrapper<AdSensitive>().eq(AdSensitive::getId, id));
        if (countID == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findAllSensitive() {
        List<AdSensitive> list = list();
        List<String> collect = list.stream().map(AdSensitive::getSensitives).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

}
